/*
** accessor for extended attribute (maybe OS dependent)
*/

#include <sys/types.h>
#include <sys/extattr.h>

#include <httpd.h>

#include <apr_pools.h>
#include <apr_strings.h>
#include <apr_xml.h>

#include "extattr.h"

static char*
get_extattr(apr_pool_t* pool, const char* path, const char* attr)
{
	char buf[1024];
	ssize_t size = extattr_get_file(path, EXTATTR_NAMESPACE_USER, attr, buf, sizeof(buf));
	if (size > 0) {
		buf[size] = '\0';
		return apr_pstrdup(pool, buf);
	} else
		return NULL;
}

void
get_extended_attribute(apr_pool_t* pool, struct exattr* exinfo, const char* fpath)
{
	exinfo->dc_title =   get_extattr(pool, fpath, KEY_TITLE);
	if (exinfo->dc_title == NULL) {
		exinfo->duration =   NULL;
		exinfo->resolution = NULL;
		exinfo->dc_date =    NULL;
		exinfo->bitrate =    NULL;
		return;
	}

	exinfo->duration =   get_extattr(pool, fpath, KEY_DURATION);
	exinfo->resolution = get_extattr(pool, fpath, KEY_RESOLUTION);
	exinfo->dc_date =    get_extattr(pool, fpath, KEY_DATE);
	exinfo->bitrate =    get_extattr(pool, fpath, KEY_BITRATE);
}
